<?php

function checkUserSession() {
    if (!$_SESSION['session_user']) {
        header('Location: login.php?user='.$_SESSION['session_user']);
        exit;
    }
}

function updateMember($member) {

    if ($member['id'] && (int)$member['id'] > 0) {

        $query = sprintf("update members set name='%s', address='%s', inn='%s', ogrn='%s', cert_num='%s', ext_info='%s' where id=%s",
            mysql_real_escape_string($member['name']),
            mysql_real_escape_string($member['address']),
            mysql_real_escape_string($member['inn']),
            mysql_real_escape_string($member['ogrn']),
            mysql_real_escape_string($member['cert_num']),
            mysql_real_escape_string($member['ext_info']),
            $member['id']
        );
        mysql_query($query);

    } else {
        $query = sprintf("insert into members (sort_order, name, address, cert_num, inn, ogrn, ext_info)
                    values (1, '%s', '%s', '%s', '%s', '%s', '%s')",
            mysql_real_escape_string($member['name']),
            mysql_real_escape_string($member['address']),
            mysql_real_escape_string($member['cert_num']),
            mysql_real_escape_string($member['inn']),
            mysql_real_escape_string($member['ogrn']),
            mysql_real_escape_string($member['ext_info'])
        );
        mysql_query($query);
    }

}

function getMember($memberId) {

    if ($memberId == 0) {
        return array();
    } else {
        $idInt = (int) $memberId;
        $result = mysql_query("select id, sort_order, name, address, inn, ogrn, cert_num, ext_info from members where id=".$idInt)
            or die('Query failed: ' . mysql_error());

        $member = array();
        if ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
            $member = array(
                "id"        => $row['id'],
                "name"      => $row['name'],
                "address"   => $row['address'],
                "inn"       => $row['inn'],
                "ogrn"      => $row['ogrn'],
                "cert_num"  => $row['cert_num'],
                "ext_info"  => $row['ext_info'],
            );
        }
        mysql_free_result($result);
        return $member;
    }

}

function getMembers() {
    $result = mysql_query("select id, sort_order, name, address, inn, ogrn, cert_num from members order by sort_order;")
        or die('Query failed: ' . mysql_error());

    $members = array();
    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
        $id = $row['id'];
        $member = array(
            "id"        => $id,
            "name"      => $row['name'],
            "address"   => $row['address'],
            "inn"       => $row['inn'],
            "ogrn"      => $row['ogrn'],
            "cert_num"  => $row['cert_num'],
        );
        $members[$id] = $member;
    }
    mysql_free_result($result);
    return $members;
}

function mySqlOpen($mySQLserver, $mySQLuser, $mySQLpassword, $database) {
    $mylink = mysql_connect($mySQLserver, $mySQLuser, $mySQLpassword) or die('Could not connect: ' . mysql_error());
    mysql_select_db($database) or die('Could not select database');
    return $mylink;
}

function mySqlClose($mylink) {
    // Closing connection
    mysql_close($mylink);
}

?>